package com.example.demo2.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .cors(cors -> cors.disable()) // 如果不需要CORS，可以选择禁用；否则，请配置适当的CORS策略
                .csrf(csrf -> csrf
                        .ignoringAntMatchers("/api/auth/**") // 忽略特定路径的CSRF检查
                )
                .authorizeRequests(authorize -> authorize
                        .antMatchers("/api/auth/register", "/api/auth/login").permitAll()
                        .anyRequest().authenticated()
                )
                .httpBasic(); // 启用基本认证

        return http.build();
    }
}